var roomGraph = argument0;
var pathGraph = argument1;
var height = array_height_2d(roomGraph);
var width = array_length_2d(roomGraph, 0);
var roomSeeds = ds_list_create();
var rooms = ds_list_create();
var minArea = 9;


//Add all room seeds to a list prioritizing start and finish seeds
for (var iii = 0; iii < height; iii++)
{
    for (var jjj = 0; jjj < width; jjj++)
    {
        var pathValue = pathGraph[iii, jjj];
        var roomValue = roomGraph[iii, jjj];
        if (roomValue == 1)
        {
            var location = 0;
            location[0] = iii;
            location[1] = jjj;
            if (is_string(pathValue) && (pathValue == 'A' || pathValue == 'B'))
            {
                ds_list_insert(roomSeeds, 0, location);
            }
            else
            {
                ds_list_add(roomSeeds, location);
            }
        }
    }
}


for (var iii = 0; iii < ds_list_size(roomSeeds); iii++)
{
    var seed = ds_list_find_value(roomSeeds, iii);
    var floorTiles = ds_list_create();
    var maxArea = irandom(20) + 9;
    var lastArea = 0;
    var area = 1;
    var iterations = 0;
    
    //Set the subgraph we will work on for each seed
    var NWBound = 0;
    if (seed[0] - 3 > -1)
    {
        NWBound[0] = seed[0] - 3;
    }
    else
    {
        NWBound[0] = 0;
    }
    
    if (seed[1] - 3 > -1)
    {
        NWBound[1] = seed[1] - 3;
    }
    else
    {
        NWBound[1] = 0;
    }
    
    var SEBound = 0;
    if (seed[0] + 3 < height)
    {
        SEBound[0] = seed[0] + 3;
    }
    else
    {
        SEBound[0] = height - 1;
    }
    
    if (seed[1] + 3 < width)
    {
        SEBound[1] = seed[1] + 3;
    }
    else
    {
        SEBound[1] = width - 1;
    }
    
    //The actual corners of the connected component
    var NWCorner = 0;
    NWCorner[0] = seed[0];
    NWCorner[1] = seed[1];
    var SECorner = 0;
    SECorner[0] = seed[0];
    SECorner[1] = seed[1];   
    
    while (area < maxArea && lastArea != area)
    {
        lastArea = area;  
        var lastNWCorner = 0;
        lastNWCorner[0] = NWCorner[1];
        lastNWCorner[1] = NWCorner[1];
        var lastSECorner = 0;
        lastSECorner[0] = SECorner[0];
        lastSECorner[1] = SECorner[1];
        
        var dir = -1; // N&S=0; E&W=1
        iterations++;
        if (iterations > 4)
        {
            dir = irandom(1);
        } 
        else
        {
            dir = iterations % 2;
        }        
        
        //North and south expansion
        if(dir == 0)
        {
            //Expand North and South
            for (var yyy = NWBound[0]; yyy <= SEBound[0] - 3 /*for s.e. height*/; yyy++)
            {
                for (var xxx = NWBound[1] + 2; xxx <= SEBound[1] - 2; xxx++)
                {
                    if (area < maxArea)
                    {
                        //Northern Expansion
                        if (roomGraph[yyy, xxx] == 0 && roomGraph[yyy + 1, xxx] == 0 && roomGraph[yyy + 2, xxx] == 0 &&
                            roomGraph[yyy + 3, xxx] != 0 && roomGraph[yyy + 1, xxx - 1] == 0 && roomGraph[yyy + 1, xxx + 1] == 0 &&
                            roomGraph[yyy + 1, xxx - 2] == 0 && roomGraph[yyy + 1, xxx + 2] == 0 && roomGraph[yyy, xxx - 1] == 0 &&
                            roomGraph[yyy, x + 1] == 0 && roomGraph[yyy, xxx - 2] == 0 && roomGraph[yyy, xxx + 2] == 0)
                        {
                            var northFloor = 0;
                            northFloor[0] = yyy + 2;
                            northFloor[1] = xxx;
                            
                            roomGraph[@ northFloor[0], northFloor[1]] = 1; //Set tile to room component
                            ds_list_add(floorTiles, northFloor); //Add tile to list for room module
                            area++;

                            //Set new corners of the connected component
                            if (northFloor[0] < NWCorner[0])
                            {
                                NWCorner[0] = northFloor[0];
                            }
                            if (northFloor[1] < NWCorner[1])
                            {
                                //should not happen but put it here anyways
                                NWCorner[1] = northFloor[1];
                            }
                        }
                        //Southern Expansion
                        else if (roomGraph[yyy, xxx] != 0 && roomGraph[yyy + 1, xxx] == 0 && roomGraph[yyy + 2, xxx] == 0 &&
                                 roomGraph[yyy + 3, xxx] == 0 && roomGraph[yyy + 2, xxx - 1] == 0 && roomGraph[yyy + 2, xxx + 1] == 0 &&
                                 roomGraph[yyy + 2, xxx - 2] == 0 && roomGraph[yyy + 2, xxx + 2] == 0 && roomGraph[yyy + 3, xxx - 1] == 0 &&
                                 roomGraph[yyy + 3, xxx + 1] == 0 && roomGraph[yyy + 3, xxx - 2] == 0 && roomGraph[yyy + 3, xxx + 2] == 0)
                        {
                            var southFloor = 0;
                            southFloor[0] = yyy + 1;
                            southFloor[1] = xxx;
                            
                            roomGraph[@  southFloor[0], southFloor[1]] = 1; //Set tile to room component
                            ds_list_add(floorTiles, southFloor);
                            area++;
                            
                            //Set new corners of the connected component
                            if (southFloor[0] > SECorner[0])
                            {
                                SECorner[0] = southFloor[0];
                            }
                            if (southFloor[1] > SECorner[1])
                            {
                                SECorner[1] = southFloor[1];
                            }
                        }
                    }
                }
            }
        }
        else if (dir == 1)
        {
            //Expand East and West
            for (var yyy = NWBound[0] + 2; yyy <= SEBound[0] - 2; yyy++)
            {
                for (var xxx = NWBound[1]; xxx <= SEBound[1] - 3 /*For S.E. width*/; xxx++)
                {
                    if (area < maxArea)
                    {
                        //Western Expansion
                        if (roomGraph[yyy, xxx] == 0 && roomGraph[yyy, xxx + 1] == 0 && roomGraph[yyy, xxx + 2] == 0 &&
                            roomGraph[yyy, xxx + 3] != 0 && roomGraph[yyy - 1, xxx + 1] == 0 && roomGraph[yyy + 1, xxx + 1] == 0 &&
                            roomGraph[yyy - 2, xxx + 1] == 0 && roomGraph[yyy + 2, xxx + 1] == 0 && roomGraph[yyy - 1, xxx] == 0 &&
                            roomGraph[yyy + 1, xxx] == 0 && roomGraph[yyy - 2, xxx] == 0 && roomGraph[yyy + 2, xxx] == 0)
                        {
                            var westFloor = 0;
                            westFloor[0] = yyy;
                            westFloor[1] = xxx + 2;
                            
                            roomGraph[@ westFloor[0], westFloor[1]] = 1; //Set tile to room component
                            ds_list_add(floorTiles, westFloor);                            
                            area++;
                            
                            //Set new corners of the connected component
                            if (westFloor[0] < NWCorner[0])
                            {
                                NWCorner[0] = westFloor[0] ;
                            }
                            if (westFloor[1] < NWCorner[1])
                            {
                                NWCorner[1] = westFloor[1];
                            }
                        }
                        //Eastern Expansion
                        else if (roomGraph[yyy, xxx] != 0 && roomGraph[yyy, xxx + 1] == 0 && roomGraph[yyy, xxx + 2] == 0 &&
                                 roomGraph[yyy, xxx + 3] == 0 && roomGraph[yyy - 1, xxx + 2] == 0 && roomGraph[yyy + 1, xxx + 2] == 0 &&
                                 roomGraph[yyy - 2, xxx + 2] == 0 && roomGraph[yyy + 2, xxx + 2] == 0 && roomGraph[yyy - 1, xxx + 3] == 0 &&
                                 roomGraph[yyy + 1, xxx + 3] == 0 && roomGraph[yyy - 2, xxx + 3] == 0 && roomGraph[yyy + 2, xxx + 3] == 0)
                        {
                            var eastFloor = 0;
                            eastFloor[0] = yyy;
                            eastFloor[1] = xxx + 1;
                            
                            roomGraph[@ eastFloor[0], eastFloor[1]] = 1; //Set tile to room component
                            ds_list_add(floorTiles, eastFloor);
                            area++;

                            //Set new corners of the connected component
                            if (eastFloor[0] > SECorner[0])
                            {
                                SECorner[0] = eastFloor[0];
                            }
                            if (eastFloor[1] > SECorner[1])
                            {
                                SECorner[1] = eastFloor[1];
                            }
                        }
                    }
                }
            }
        }
        
        //Update boundaries
        //If we created a new corner
        if (NWCorner[0] != lastNWCorner[0])
        {
            if (NWCorner[0] - 3 > -1)
            {
                NWBound[0] = NWCorner[0] - 3;
            }
            else
            {
                NWBound[0] = 0;
            }
        }
        
        if (NWCorner[1] != lastNWCorner[1])
        {
            if (NWCorner[1] - 3 > -1)
            {
                NWBound[1] = NWCorner[1] - 3; 
            }
            else
            {
                NWBound[1] = 0;
            }
        }
        
        if (SECorner[0] != lastSECorner[1])
        {
            if(SECorner[0] + 3 < height)
            {
                SEBound[0] = SECorner[0] + 3;
            }
            else
            {
                SEBound[0] = height - 1;
            }
        }
        
        if (SECorner[1] != lastSECorner[1])
        {
            if (SECorner[1] + 3 < width)
            {
                SEBound[1] = SECorner[1] + 3;
            }
            else
            {
                SEBound[1] = width - 1;            
            }
        }         
    }    
    
    //Erase room if not big enough
    if (area < minArea)
    {
        for (var aaa = NWBound[0]; aaa <= SEBound[0]; aaa++)
        {
            for (var bbb = NWBound[1]; bbb <= SEBound[1]; bbb++)
            {
                roomGraph[@ aaa, bbb] = 0;
            }
        }
    }
    
    //Create a room module  
    else 
    {        
        //add seed tile to list
        ds_list_add(floorTiles, seed);        
        ds_list_add(rooms, roomFactory(NWCorner, floorTiles));
    }
    
    ds_list_destroy(floorTiles);
}

ds_list_destroy(roomSeeds);

roomsArray = 0;
for (var i = 0; i < ds_list_size(rooms); i++)
{
    roomsArray[i] = ds_list_find_value(rooms, i);
}

ds_list_destroy(rooms);

return roomsArray;


